## R Script Output -------------------------------------------------------------
# Appendix Figure A4: Chinese FREI Exposure in the Greater Los Angeles Area by ZIP Codes


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",
         "RColorBrewer", 
         "gridExtra", 
         "viridis",
         "choroplethrZip")
  
lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/ISQ-frei-replication/"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "foia-zcta-cn-under-2012.RData", sep = ""))


## Figure A4 -------------------------------------------------------------------
# set regions
la.fips <- c(06037, 06059, 06111, 06071, 06065)

# set parameters
cut.points <- c(0, 1, 10, 
                25, 50, 100, 150, 
                250, 500, Inf)

cut.labels <- c("0", "[1, 10)", "[10, 25)",
                "[25, 50)", "[50, 100)", 
                "[100, 150)", "[150, 250)", 
                "[250, 500)", "> 500")

n.colors <- 8

# prep df for plotting
plot.df <- foia.idw.cn.und.2012 %>%
  rename(region = zcta,
         value = wt_total) %>%
  mutate(value = cut(value, breaks = cut.points, 
                     right = FALSE,
                     labels = cut.labels))

# plot
pdf(file = paste(MAIN_DIR, "Figure-A4.pdf", sep = ""),
    width = 12, height = 10)
zip_choropleth(df = plot.df, 
               title = " Inverse Distance Weighted Total CHN Undergraduate Students in LA, 2012",
               legend = "Total",
               num_colors = n.colors,
               county_zoom = la.fips) +
  scale_fill_manual(values = viridis(n.colors + 1, 
                                     alpha = 1, begin = 0, end = 1, 
                                     option = "A")[2:(n.colors + 1)],
                    name = "Weighted Population",
                    na.value = "grey75")
dev.off()

